﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace RecruitmentProcessSystem
{
    public partial class frmVacancyManager : Form
    {
        String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection conn;
        SqlCommand cmd, cmd_add, cmd_edit, cmd_del;
        SqlDataAdapter da;
        DataSet ds;
        public frmVacancyManager()
        {
            InitializeComponent();
        }


        private void frmVacancyManager_Load(object sender, EventArgs e)
        {
            btEdit.Enabled = false;
            btDelete.Enabled = false;
            ds = new DataSet();
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd = new SqlCommand("Select * from Vacancy", conn);
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Vacancy");
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btCreate_Click(object sender, EventArgs e)
        {
            if (txtvName.Text == "")
            {
                MessageBox.Show("Nhập vị trí!");
            }else
            if (txtvDepartment.Text == "")
            {
                MessageBox.Show("Nhập phòng ban!");
            }else
            if (txtvNumber.Text == "")
            {
                MessageBox.Show("Nhập số lượng!");
            }else
                if (txtvStatus.Text == "")
                {
                    MessageBox.Show("Nhập trạng thái!");
                }
                else
                {
                    conn = new SqlConnection(conStr);
                    conn.Open();
                    cmd_add = new SqlCommand("insert into Vacancy(vname,vdepartment,vnumber,vstatus) values('" + txtvName.Text + "','" + txtvDepartment.Text + "','" + txtvNumber.Text + "','" + txtvStatus.Text + "')", conn);
                    cmd_add.ExecuteNonQuery();
                    conn.Close();
                    this.frmVacancyManager_Load(sender, e);
                    MessageBox.Show("Creat new successfully!");
                }
        }
      

        private void btEdit_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd_edit = new SqlCommand("update Vacancy set vname = '" + txtvName.Text + "',vDepartment = '" + txtvDepartment.Text + "',vNumber = '" + txtvNumber.Text + ",vStatus = '" + txtvStatus.Text + "' where vid = " + txtvID.Text, conn);
            cmd_edit.ExecuteNonQuery();
            conn.Close();
            this.frmVacancyManager_Load(sender, e);
            MessageBox.Show("Update successfully!");
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to delete?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                conn = new SqlConnection(conStr);
                conn.Open();
                cmd_del = new SqlCommand("Delete from Vacancy where vid = '" + txtvID.Text + "'", conn);
                cmd_del.ExecuteNonQuery();
                conn.Close();
                this.frmVacancyManager_Load(sender, e);
                MessageBox.Show("Delete successfully!");
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                btEdit.Enabled = true;
                btDelete.Enabled = true;
                DataGridViewRow row = new DataGridViewRow();
                row = dataGridView1.Rows[e.RowIndex];
                txtvID.Text = row.Cells[0].Value.ToString();
                txtvName.Text = row.Cells[1].Value.ToString();
                txtvDepartment.Text = row.Cells[2].Value.ToString();
                txtvNumber.Text = row.Cells[3].Value.ToString();
                txtvStatus.Text = row.Cells[4].Value.ToString();
            }
            catch
            {

            }

        }  
    }
}
